import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class E05_FibonacciCallable {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();

        ArrayList<Future<String>> result = new ArrayList<Future<String>>();

        for (int i = 0; i < 10; i++) {
            result.add(exec.submit(new Fibonacci2(i)));
        }

        for (Future<String> fs : result) {
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } finally {
                exec.shutdown();
            }
        }
    }
}

class Fibonacci2 implements Callable<String> {

    private int n;

    public Fibonacci2(int n) {
        this.n = n;
    }

    @Override
    public String call() throws Exception {
        Integer[] seq = new Integer[n];
        for (int i = 0; i < n; i++) {
            seq[i] = fib(i);
        }

        return "seq " + n + ": " + Arrays.toString(seq);
    }

    public int fib(int n) {
        if (n < 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }
}